<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests\ShopDoaddRequest;  //引入分类输入验证的文件 ,注意命名空间
use DB;  //引入DB类文件

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ShopController extends Controller
{
    // 商店列表
    public function getIndex(Request $request)
    {
        // 判断是否有查询条件
        
        if($request->input('keywords')){
            $shops = DB::table('shops')->where('sname','like','%'.$request->input('keywords').'%')->paginate(15);
        }else{
            $shops = DB::table('shops')->paginate(15);  // 分页
        }

        $list = $request->all();  //获取条件参数,用于下一页时传参
        // dd();
        // dd($list);
        return view('admin.shops.index',['shops'=>$shops,'list'=>$list]);  //解析模板 分配数据
    }

    // 封装一个方法用来处理分类图片上传
    private function upload($request,$file)
    {
        if($request->hasFile($file)){
            $suffix = $request->file($file)->getClientOriginalExtension(); // 获取文件后缀名
            $name = md5(time()).rand(0,99999);  //生成一个随机文件名
            $request->file($file)->move('./uploads/shopimg/',$name.'.'.$suffix);  //移动
            return '/uploads/shopimg/'.$name.'.'.$suffix;  //返回路径信息
        }
    }

    // 添加商店
    public function getAdd()
    {
    	return view('admin.shops.add');
    }

    // 处理添加商店信息
    public function postDoadd(ShopDoaddRequest $request)
    {
        // dd($request->all());
        $data = $request->except('_token');

        $data['spic'] = $this->upload($request,'spic');  //调用函数处理图片
        var_dump($data['spic']);die;
    	$res = DB::table('shops')->insert($data);
    	if($res){
    		return redirect('/admin/shop/index')->with('success','添加分类成功');
    	}else{
    		return back()->with('error','添加分类失败');
    	}
    }

    // 删除
    public function postDel(Request $request)
    {
        // 判断该商店下是否有商品
        $res1 = DB::select('select * from goods where sid = '.$request->input('sid'));
        if($res1){
            echo 1;return; //该商店下有商品
        }

        // 执行删除操作
        $res2 = DB::table('shops')->where('sid',$request->input('sid'))->delete();  //删除用户
        if($res2){
            echo 2; //返回  ajax判断
        }
    }

    // 修改商店时的表单验证AJAX
    public function getEditcheck()
    {
        $sid = $_GET['sid'];  //获取id
        $sname = $_GET['sname'];  //获取sname

        $oldsname = DB::select('select sname from shops where sid = '.$sid)[0]->sname;

        // 判断商店名字是否修改 
        if($oldsname == $sname){
            echo 2;die;
        }

        $res = DB::select('select sname from shops where sname = "'.$sname.'"');
        if($res){
            echo 1;
        }
    }

    // 修改商店信息
    public function getEdit(Request $request)
    {
        // dd($request->all());
        // 查询需要修改的分类信息
        $sid = $request->id;  //获取id
        $shops = DB::table('shops')->where('sid',$sid)->first(); 
        return view('admin.shops.edit',['shops'=>$shops]);
    }

    // 处理修改分类信息
    public function postDoedit(Request $request)
    {
        // dd($request->all());
        $data = $request->except('_token','oldpic','oldsname');

        // 判断是否修改图片
        if(empty($request->spic)){
            $data['spic'] = $request->oldpic;
        }else{
            $data['spic'] = $this->upload($request,'spic');  //调用函数处理图片
        }

        // dd($data);
        $res = DB::table('shops')->where('sid',$request->sid)->update($data);
        if($res){
            return redirect('/admin/shop/index')->with('success','修改分类成功');
        }else{
            return back()->with('error','修改分类失败');
        }
    }
}
